######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate a site collection
# -----------------------------------------------------------------
function New-SiteCollection()
{
    $obj=new-object PSObject -Property @{
        RelativeUrl = "";
        Title = "";
        Description = "";
        Visitors = @();
        Members = @();
        Owners = @();
        PrimaryOwner = "";
        SecondaryOwner = "";
        Features = @();
        Template = "";
        WebApplication = "";
        Language = 1033;
    }
	Add-Property $obj "AbsoluteUrl" `
	{
		return $this.WebApplication.DefaultZone.AbsoluteUrl + $this.RelativeUrl
	}
	
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
        Write-Operation "    Site Collection"  
        Validate-NotEmpty "Title" $this.Title
        Validate-NotEmpty "Template" $this.Template
        Validate-NotEmpty "PrimaryOwner" $this.PrimaryOwner 
    }
    # -----------------------------------------------------------------
    # Provisions the site collection
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    { 
        $sc = Get-SPSite -Limit All -WebApplication $this.WebApplication.DefaultZone.DefaultUrl | Where-Object {$_.Url -eq $this.AbsoluteUrl}
		
        if($sc -eq $null)
        {
            Write-Operation "Creating Site Collection $($this.AbsoluteUrl)"
            if($this.SecondaryOwner -eq "")
            {
                $sc = New-SPSite -Name $this.Title `
						-Description $this.Description `
						-OwnerAlias $this.PrimaryOwner `
						-Template $this.Template `
						-Url $this.AbsoluteUrl `
						-Language $this.Language
            }
            else
            {
                $sc = New-SPSite -Name $this.Title `
						-Description $this.Description `
						-OwnerAlias $this.PrimaryOwner `
						-SecondaryOwnerAlias $this.SecondaryOwner `
						-Template $this.Template `
						-Url $this.AbsoluteUrl `
						-Language $this.Language
            }
			$sc.Dispose()
        }
        else
        {
			$sc.Dispose()
            Write-OperationDone "Site collection $($this.AbsoluteUrl) already exists"
        }
    }
    # -----------------------------------------------------------------
    # Activates Web Application scoped features
    # -----------------------------------------------------------------
    Add-Method $obj "ActivateFeatures" `
    {   		
        foreach ($name in $this.Features) 
        {
            if($name -ne "")
            {
				$site = Get-SPSite -Identity $this.AbsoluteUrl
			
                $f = Get-SPFeature -Site $site | Where-Object {$_.DisplayName -eq $name }
                if($f -eq $null)
                {
                    Write-Operation "Activating feature $($name) at $($this.AbsoluteUrl)"
                    Enable-SPFeature -Identity $name -Url $this.AbsoluteUrl
                }
				$site.Dispose()
            }
        }
    }
    # -----------------------------------------------------------------
    # Configures access to the site collection
    # -----------------------------------------------------------------
    Add-Method $obj "ConfigureAccess" `
    {
		$site = Get-SPSite -Identity $this.AbsoluteUrl
        AddUsersToGroup $this.Visitors $site.RootWeb.AssociatedVisitorGroup
        AddUsersToGroup $this.Members $site.RootWeb.AssociatedMemberGroup
        AddUsersToGroup $this.Owners $site.RootWeb.AssociatedOwnerGroup
        $site.Dispose()
    }
    return $obj
}
